<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace hdphp\traits;

use app\dao\BaseDao;
use think\helper\Str;

/**
 * Trait：SearchDaoTrait
 * 创建人：Jack
 * 创建时间：2023/11/28 10:37
 * @package hdphp\traits
 * @mixin BaseDao
 */
trait SearchDaoTrait
{
    /**
     * 注释：搜索（没有进入搜索器的自动进入where条件）
     * 创建人：Jack
     * 创建时间：2023/12/1 17:51
     * @param array $where
     * @param bool  $authWhere
     * @return \hdphp\basic\BaseModel
     * @throws \ReflectionException
     */
    public function searchWhere(array $where, bool $authWhere = true)
    {
        $with = [];
        $whereKey = [];
        $respones = new \ReflectionClass($this->setModel());
        foreach (array_keys($where) as $fieldName) {
            $method = 'search' . Str::studly($fieldName) . 'Attr';
            if ($respones->hasMethod($method)) {
                $with[] = $fieldName;
            } else {
                $whereKey[] = $fieldName;
            }
        }

        $whereData = [];
        foreach ($whereKey as $key) {
            if (isset($where[$key]) && 'timeKey' !== $key) {
                $whereData[$key] = $where[$key];
            }
        }

        return $this->getModel()->withSearch($with, $where)->when($authWhere && $whereData, function ($query) use ($whereData) {
            $query->where($whereData);
        });
    }


    /**
     * 注释：count
     * 创建人：Jack
     * 创建时间：2023/11/28 10:38
     * @param array $where
     * @param bool  $authWhere
     * @return int
     */
    public function count(array $where = [], bool $authWhere = true): int
    {
        return $this->searchWhere($where, $authWhere)->count();
    }

    /**
     * 搜索
     * 注释：getDataList
     * 创建人：Jack
     * 创建时间：2023/11/28 10:38
     * @param array          $where
     * @param array|string[] $field
     * @param int            $page
     * @param int            $limit
     * @param                $sort
     * @param array          $with
     * @return mixed
     */
    public function getDataList(array $where, array $field = ['*'], int $page = 0, int $limit = 0, $sort = null, array $with = [])
    {
        return $this->searchWhere($where)->when($page && $limit, function ($query) use ($page, $limit) {
            $query->page($page, $limit);
        })->when(!$page && $limit, function ($query) use ($limit) {
            $query->limit($limit);
        })->when($sort, function ($query, $sort) {
            if (is_array($sort)) {
                foreach ($sort as $k => $v) {
                    if (is_numeric($k)) {
                        $query->order($v, 'desc');
                    } else {
                        $query->order($k, $v);
                    }
                }
            } else {
                $query->order($sort, 'desc');
            }
        })->field($field)->with($with)->select()->toArray();
    }

}